﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using System.IO;

namespace WebUI.Controls
{
    public partial class EditProductCtrl : BaseEditControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Init();

            if (!Branches.CurrentBranch.IsHeadQuarter)
            {
                panelExtra.Visible = false;
                lblRegularPrice.Visible = false;
                txtRegularPrice.Visible = false;
            }
        }

        public void Init()
        {
            View_CategoryTree categories = View_CategoryTree.GetCategoryTree();
            Utilities.LoadDropDownList(ddlCategory, categories.DefaultView, View_CategoryTree.ColumnNames.Path, View_CategoryTree.ColumnNames.ID);

            Brands brands = Brands.LoadAllActiveBrands();
            Utilities.LoadDropDownList(ddlBrand, brands.DefaultView, Brands.ColumnNames.BrandName, Brands.ColumnNames.ID);
        }

        public string ProductName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public int CategoryID
        {
            get { return ddlCategory.SelectedIndex >= 0 ? int.Parse(ddlCategory.SelectedValue) : -1; }
            set { ddlCategory.SelectedValue = value.ToString(); }
        }

        public int BrandID
        {
            get { return ddlBrand.SelectedIndex >= 0 ? int.Parse(ddlBrand.SelectedValue) : -1; }
            set { ddlBrand.SelectedValue = value.ToString(); }
        }

        public bool IMEIAvailable
        {
            get { return chkIMEI.Checked; }
            set { chkIMEI.Checked = value; txtQuantity.Enabled = !value; }
        }

        public string BarCode
        {
            get { return txtBarCode.Text; }
            set { txtBarCode.Text = value;ShowBarCode(); }
        }

        public string Picture
        {
            get { return hidPicture.Value; }
            set 
            { 
                hidPicture.Value = value;
                lnkPicture.NavigateUrl = value;
            }
        }

        public string Thumbnail
        {
            get { return hidThumbnail.Value; }
            set
            {
                hidThumbnail.Value = value;
                imgPicture.ImageUrl = value;
            }
        }

        public int Quantity
        {
            get
            {
                try
                {
                    return int.Parse(txtQuantity.Text);
                }
                catch (Exception ex)
                {
                    throw (new Exception("Invalid Quantity!"));
                }
            }
            set
            {
                txtQuantity.Text = value.ToString();
            }
        }

        public int SoldQuantity
        {
            get
            {
                try
                {
                    return int.Parse(txtSoldQuantity.Text);
                }
                catch (Exception ex)
                {
                    throw (new Exception("Invalid Sold Quantity!"));
                }
            }
            set
            {
                txtSoldQuantity.Text = value.ToString();
            }
        }

        public decimal Cost
        {
            get
            {
                try
                {
                    return decimal.Parse(txtCost.Text);
                }
                catch (Exception ex)
                {
                    throw (new Exception("Invalid Cost!"));
                }
            }
            set
            {
                txtCost.Text = value.ToString();
            }
        }

        public decimal RegularPrice
        {
            get
            {
                try
                {
                    if (txtRegularPrice.Text.Trim() == "")
                        return -1;
                    return decimal.Parse(txtRegularPrice.Text.Trim());
                }
                catch (Exception ex)
                {
                    throw (new Exception("Invalid Regular Price!"));
                }
            }
            set
            {
                txtRegularPrice.Text = value.ToString();
            }
        }

        public decimal DealerPrice
        {
            get
            {
                try
                {
                    return decimal.Parse(txtDealerPrice.Text);
                }
                catch (Exception ex)
                {
                    throw (new Exception("Invalid Dealer Price!"));
                }
            }
            set
            {
                txtDealerPrice.Text = value.ToString();
            }
        }

        public decimal PartnerPrice
        {
            get
            {
                try
                {
                    return decimal.Parse(txtPartnerPrice.Text);
                }
                catch (Exception ex)
                {
                    throw (new Exception("Invalid Partner Price!"));
                }
            }
            set
            {
                txtPartnerPrice.Text = value.ToString();
            }
        }

        public decimal SalePrice
        {
            get
            {
                try
                {
                    return decimal.Parse(txtSalePrice.Text);
                }
                catch (Exception ex)
                {
                    throw (new Exception("Invalid Sale Price!"));
                }
            }
            set
            {
                txtSalePrice.Text = value.ToString();
            }
        }

        public bool GST
        {
            get { return chkGST.Checked; }
            set { chkGST.Checked = value; }
        }

        public bool HotProduct
        {
            get { return chkHotProduct.Checked; }
            set { chkHotProduct.Checked = value; }
        }

        public bool NewArrival
        {
            get { return chkNewArrival.Checked; }
            set { chkNewArrival.Checked = value; }
        }

        public bool HomeVisible
        {
            get { return chkHome.Checked; }
            set { chkHome.Checked = value; }
        }

        public bool DealerOnly
        {
            get { return chkDealerOnly.Checked; }
            set { chkDealerOnly.Checked = value; }
        }

        public void ShowBarCode()
        {
            if (!string.IsNullOrEmpty(BarCode))
                divBarCode.InnerHtml = Utilities.GenerateBarCode(BarCode);
            else
                divBarCode.InnerHtml = "";
        }

        protected void btnUploadFinish_OnClick(object sender, EventArgs e)
        {
            Picture = Server.UrlDecode(hidPicture.Value);
            Thumbnail = Server.UrlDecode(hidThumbnail.Value);
        }
    }
}